package ccm.component.menu;

import ccm.common.Buffer;
import ccm.common.CCMConfigManager;
import ccm.common.Crc;
import ccm.component.override.JCheckBox;
import ccm.component.override.JLabel;
import ccm.component.override.JMenuItem;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;

abstract class MenuItemJPanel extends JPanel
{
    //TODO 合并 MenuItemJPanel
    static protected final Font font=new Font("Consolas",Font.PLAIN,16);
    static protected final Font fontChinese=new Font("仿宋",Font.PLAIN,16);
    protected final JLabel nameJLabel;
    protected final JCheckBox activateJCheckbox;
    private final DelegateSenderInterface sender;
    protected boolean canModify;
    protected ScnsType type;
    protected int dataIndex;
    protected int page;
    protected String name;
    protected boolean needRequest;
    protected boolean needRequestValue;
    protected String saveArgPrefix;
    protected final JMenuItem clipJMenuItem;

    public MenuItemJPanel(DelegateSenderInterface sender,String saveArgPrefix,int dataIndex,boolean canModify,ScnsType type,int page,String name)
    {
        super();
        this.sender=sender;
        setLayout(new FlowLayout(FlowLayout.LEFT));
        nameJLabel=new JLabel();
        activateJCheckbox=new JCheckBox("激活");
        add(activateJCheckbox);
        add(nameJLabel);
        this.saveArgPrefix=saveArgPrefix;
        this.dataIndex=dataIndex;
        this.type=type;
        nameJLabel.setToolTipText(name+":"+type.toString());
        setCanModify(canModify);
        setPage(page);
        setName(name);
        setActivateEnable(true);
        activateJCheckbox.addActionListener(actionEvent->storeConfig());
        loadConfig();
        Dimension dimension=new Dimension(150,(int)(nameJLabel.getFont().getSize()*1.2+10));
        nameJLabel.setPreferredSize(dimension);
        nameJLabel.setBorder(BorderFactory.createEtchedBorder());
        activateJCheckbox.setBorder(BorderFactory.createEtchedBorder());

        clipJMenuItem=new JMenuItem("复制到剪切板");
        clipJMenuItem.addActionListener(e->
        {
            final StringSelection data=new StringSelection(__toString());
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(data,data);
        });
        MouseAdapter mouseAdapter=new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                if(e.getButton()==MouseEvent.BUTTON1&&e.getClickCount()>=2)
                {
                    final StringSelection data=new StringSelection(__toString());
                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(data,data);
                }
            }
        };
        nameJLabel.addMouseListener(mouseAdapter);
        this.addMouseListener(mouseAdapter);
    }

    public void setActivateEnable(boolean enable)
    {
        activateJCheckbox.setText(enable?"激活":("P"+page));
    }

    protected String storeConfig()
    {
        final String prefix=saveArgPrefix+"ccm/component/menu/MenuJPanel/item/"+page+','+name;
        CCMConfigManager.setBoolean(prefix+"/activate",activateJCheckbox.isSelected());
        return prefix;
    }

    public String loadConfig()
    {
        final String prefix=saveArgPrefix+"ccm/component/menu/MenuJPanel/item/"+page+','+name;
        activateJCheckbox.setSelected(CCMConfigManager.setBooleanIfAbsent(prefix+"/activate",false));
        return prefix;
    }

    public boolean isActivate()
    {
        return activateJCheckbox.isSelected();
    }

    public boolean isCanModify()
    {
        return canModify;
    }

    public void setCanModify(boolean canModify)
    {
        this.canModify=canModify;
    }

    public int getPage()
    {
        return page;
    }

    public void setPage(int page)
    {
        this.page=page;
    }

    @Override
    public String getName()
    {
        return name;
    }

    @Override
    public void setName(String name)
    {
        this.name=name;
        this.nameJLabel.setText(this.name);
        this.nameJLabel.setToolTipText(name+":"+type.toString());
    }

    public ScnsType getType()
    {
        return type;
    }

    public int getDataIndex()
    {
        return dataIndex;
    }

    /**
     * 设置参数保存前缀
     * 会自动刷新所有的参数
     *
     * @param saveArgPrefix 参数保存前缀
     */
    public void setSaveArgPrefix(String saveArgPrefix)
    {
        this.saveArgPrefix=saveArgPrefix==null?"":(saveArgPrefix.endsWith("/")?saveArgPrefix:(saveArgPrefix+"/"));
    }

    protected void addTailAndSend(byte[] buf,int start,int type)
    {
        if(buf!=null)
        {
            start=Buffer.setUint8(buf,start,start);
            start=Buffer.setUint16(buf,start,Crc.crc16Modbus(buf,0,start));
            buf[start]=0X13;
            buf[start+1]=0X18;
            buf[start+2]=(byte)type;
            sender.sendData(Arrays.copyOfRange(buf,0,start+3));
        }
    }

    public abstract String toString();

    private String __toString()
    {
        return toString();
    }

    public void setStep(float step)
    {
    }
}
